//
//  BDPayAddressChooseView.m
//  BadBoy
//
//  Created by wen on 2017/5/6.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDPayAddressChooseView.h"
#import "BDMalAddressTableViewCell.h"

@interface BDPayAddressChooseView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *datasource;

@end


@implementation BDPayAddressChooseView

- (instancetype)initWithFrame:(CGRect)frame addressArray:(NSArray *)addressArray{
    if (self = [super initWithFrame:frame]) {
        self.datasource = addressArray;
        UILabel *label = [[UILabel alloc] init];
        label.text = ASLocalizedString(@"选择地址");
        label.font = [UIFont systemFontOfSize:16];
        label.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
        [label sizeToFit];
        label.centerX = kBDScreenWidth / 2.0;
        label.height = 45;
        [self addSubview:label];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0.5)];
        line.backgroundColor = kBDColorGray;
        line.top = label.bottom;
        [self addSubview:line];
        
        self.tableView.height = self.height - line.bottom;
        self.tableView.top = line.bottom;
        [self addSubview:self.tableView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BDMalAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bdAddressCell"];
    if (cell == nil) {
        cell = [[BDMalAddressTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bdAddressCell"];
    }
    [cell configeWithModel:self.datasource[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.finishBlock) {
        self.finishBlock(self.datasource[indexPath.row]);
    }
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 70)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return _tableView;
}

@end
